package com.gyms.activity;

import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.classic.okhttp.enums.Enum;
import com.classic.okhttp.services.IUserService;
import com.classic.okhttp.zmoumall.http.ObjectCallback;
import com.classic.okhttp.zmoumall.utils.MD5;
import com.classic.okhttp.zmoumall.utils.ToastUtil;
import com.gyms.R;
import com.gyms.base.MyAutoLayoutActivity;
import com.gyms.constant.Constants;
import com.gyms.manager.DataCenter;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;
import constant.EventConstant;
import utils.StringUtils;
import weight.UiEvent;

/**
 * 第一次修改密码 yjl
 */
public class FirstEditPassActivity extends MyAutoLayoutActivity implements View.OnClickListener {
    @BindView(R.id.et_newpass)
    EditText etNewpass;
    @BindView(R.id.et_newpass_again)
    EditText etNewpassAgain;
    @BindView(R.id.tv_gym_phone_big)
    TextView tvGymPhoneBig;
    @BindView(R.id.iv_back)
    ImageView mIvBack;

    String passWord,conFimPass;
    private boolean isNeedmodify; //是否需要修改 如果还没设置密码就是true

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_first_edit_pass;
    }

    @Override
    protected void initView() {
        initData();
        setTitle("设置密码");
//        setBack();
        if(isNeedmodify){//设置成叉叉
            mIvBack.setImageResource(R.drawable.city_close_selector);
            mIvBack.setVisibility(View.VISIBLE);
        }else {
            setBack();
        }
        createLoadingView("请求中",true);
    }

    private void initData() {
        Bundle mBudnle=getIntent().getExtras();
        if(null!=mBudnle){
            isNeedmodify=mBudnle.getBoolean(Constants.KEY_MUST_MODIFY);
        }
    }

    @Override
    protected void setListener() {
        mIvBack.setOnClickListener(this);
    }

    @Override
    protected void processLogic() {

    }


    @OnClick(R.id.tv_gym_phone_big)
    public void onClick() {
        passWord=etNewpass.getText().toString().trim();
        conFimPass=etNewpassAgain.getText().toString().trim();
        if(StringUtils.isEmpty(passWord)||StringUtils.isEmpty(conFimPass)){
            ToastUtil.showToast(mContext,"请输入密码");
            return;
        }
        if(!passWord.equals(conFimPass)){
            ToastUtil.showToast(mContext,"两次密码不一致");
            return;
        }
        String pass= MD5.MD5Encode(new StringBuffer(conFimPass).toString());
        loadingView.show();
        IUserService.modifyUser(this, Enum.EnumUserFieldType.Password, pass, false, new ObjectCallback<Object>() {
            @Override
            public void onSuccess(Object response, String message) {
                dialogDis();
                ToastUtil.showToast(mContext,"设置密码成功");
                DataCenter.getInstance().getUser().setIsSetPwd(true);
                //刷新提示
                EventBus.getDefault().post(new UiEvent(EventConstant.S_PASS, ""));
                finish();
            }

            @Override
            public void onErrorT(int code, Object response, String errorMessage) {
                dialogDis();
                ToastUtil.showToast(mContext,errorMessage);
            }

            @Override
            public void onHttpErrorCall(int statusCode) {
                dialogDis();
            }
        });
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        hideKeyBoard(ev);//隐藏软键盘
        return super.dispatchTouchEvent(ev);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_back:
                finish();
                if(isNeedmodify){
                    overridePendingTransition(R.anim.activity_close_in, R.anim.activity_close_out);
                }
                break;
        }
    }
}
